﻿using System.Globalization;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using EFERP;
using HRM.DICTIONARY.DAL;
using System;
using System.Windows.Forms;

namespace HRM.DICTIONARY.UI
{
    public partial class xucSalaryStepAdd : DevExpress.XtraEditors.XtraForm
    {
       private dic_salary_step _obj;
        private readonly ICommon<dic_salary_step> _exeDic;
        private readonly ICommon<dic_salary_rank> _exeDicRank;
        private CloseOrNew _closeNew = CloseOrNew.Close;

        public xucSalaryStepAdd(ICommon<dic_salary_step> exeDic)
        {
            InitializeComponent();
            this._exeDicRank = new Common<dic_salary_rank>(StaticVariable.ERPEn);
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Insert;
            this._obj = new dic_salary_step();
            SetDataIntoGridLookup();
            SetDataIntoUI();
        }

        public xucSalaryStepAdd(dic_salary_step currObj, ICommon<dic_salary_step> exeDic)
        {
            InitializeComponent();
            this._exeDicRank = new Common<dic_salary_rank>(StaticVariable.ERPEn);this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Update;
            this._obj = currObj;
            SetDataIntoGridLookup();
            SetDataIntoUI();
        }

        private void xucSalaryStepAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            TotalClose();
        }

        private void SetDataIntoUI()
        {
            if (_obj == null)
                return;
            switch (_exeDic.myAction){
                case Actions.Update:
                    txtMa.Text = _obj.stepcode.ToString(CultureInfo.InvariantCulture);
                    txtTen.Text = _obj.stepname;
                    txtMoTa.Text = _obj.description;
                    calHeSo.EditValue = _obj.coefficient;
                    glkNgachLuong.EditValue = _obj.rankcode;
            

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = false;
                    break;
                case Actions.Insert:
                    _obj = new dic_salary_step();
                    txtMa.Text = _exeDic.NewID("");
                    txtTen.Text = "";
                    txtMoTa.Text = "";
                    calHeSo.EditValue = 0;
                    glkNgachLuong.EditValue = "";


                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
                case Actions.InsertMultiple:
                    _obj = new dic_salary_step();
                    txtMa.Text = _exeDic.NewID("");
                    txtTen.Text = "";
                    txtMoTa.Text = "";
                    calHeSo.EditValue = 0;
                    glkNgachLuong.EditValue = "";

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
            }
        }

        private bool SetUIIntoData()
        {
            try
            {
                if (txtMa.Text == "" || txtTen.Text == "")
                {
                    MessageBox.Show(@"Vui lòng không để trống thông tin bậc lương");
                    return false;
                }
                _obj.stepcode = int.Parse(txtMa.Text);
                _obj.stepname = txtTen.Text;
                _obj.description = txtMoTa.Text;
                _obj.coefficient = double.Parse(calHeSo.EditValue.ToString());
                _obj.rankcode = glkNgachLuong.EditValue.ToString();

                return true;
            }catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return false;
        }

         //Add them if we have to combine the other tables to get data
        private bool SetDataIntoGridLookup()
        {
            bool kq = SetDataSysBrandIntoGridLookup();
            if (kq) return true;
            XtraMessageBox.Show(_exeDicRank.ErrMsg, "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return false;
        }

        private bool SetDataSysBrandIntoGridLookup()
        {
            bool kq = _exeDicRank.AddGridLookupEdit(glkNgachLuong, "rankname", "rankcode");
            if (!kq) return false;
            //if (glkNgachLuong.Properties.DataSource != null)
            //{
            //    glkNgachLuong.EditValue = @"";
            //}
            return true;
        }

        private void TotalControl()
        {
            bool kq;
            switch (_exeDic.myAction)
            {
                case Actions.Insert:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Insert;
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;

                case Actions.Update:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.UpdateOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Update;
                            this.Close();
                        }
                        else
                        {
                            _exeDic.ValueOfGridViewRow = null;
                            MessageBox.Show(@"Lỗi cập nhật dữ liệu vào hệ thống");
                        }
                    }
                    break;
                case Actions.InsertMultiple:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            _exeDic.myAction = Actions.InsertMultiple;
                            SetDataIntoUI();
                            this.DialogResult = DialogResult.OK;
                            if (_closeNew == CloseOrNew.Close)
                                this.Close();
                            else
                                _closeNew = CloseOrNew.New;
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;
            }
        }
        private void TotalClose()
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            TotalClose();
        }

        private void btnLuuDong_Click(object sender, EventArgs e)
        {
            _closeNew = CloseOrNew.Close;
            TotalControl();
        }

        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            _exeDic.myAction = Actions.InsertMultiple;
            _closeNew = CloseOrNew.New;
            TotalControl();
        }

        private void glkNgachLuong_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind != ButtonPredefines.Plus) return; xucSalaryRankAdd add = new xucSalaryRankAdd(_exeDicRank);
            add.ShowDialog();
        }
    }
}